package com.tfswue.nyf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tfswue.nyf.entity.Order;
import com.tfswue.nyf.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单业务逻辑层
 */
@Service
public class OrderService {
    
    @Autowired
    private OrderMapper orderMapper;
    
    /**
     * 创建订单
     */
    public Order createOrder(Order order) {
        orderMapper.insert(order);
        return order;
    }
    
    /**
     * 根据ID查询订单
     */
    public Order findById(Long id) {
        return orderMapper.selectById(id);
    }
    
    /**
     * 查询所有订单
     */
    public List<Order> findAll() {
        return orderMapper.selectList(null);
    }
    
    /**
     * 根据用户ID查询订单
     */
    public List<Order> findByUserId(Long userId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return orderMapper.selectList(queryWrapper);
    }
    
    /**
     * 更新订单
     */
    public Order updateOrder(Order order) {
        orderMapper.updateById(order);
        return order;
    }
    
    /**
     * 删除订单
     */
    public void deleteOrder(Long id) {
        orderMapper.deleteById(id);
    }
    
    /**
     * 检查订单是否存在
     */
    public boolean existsById(Long id) {
        return orderMapper.selectById(id) != null;
    }
}